package com.hadoop.hive;

import java.sql.*;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2017/10/27
 */
public class HiveJdbcConnect {

    private static final String JDBC_NAME = "org.apache.hive.jdbc.HiveDriver";

    public static void main(String[] args) throws SQLException {

        try {
            Class.forName(JDBC_NAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }

        Connection conn = DriverManager.getConnection("jdbc:hive2://master:10000/default", "root", "");
        Statement state = conn.createStatement();
        String tableName = "tb_tt";
        /*state.execute("DROP TABLE IF EXISTS " + tableName);
        state.execute("CREATE TABLE " + tableName + "(key INT ,value string)");
        System.out.println("create table success!");

        //show tables
        String sql = "SHOW TABLES " + tableName;
        System.out.println("Running " + sql);

        ResultSet resultSet = state.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }

        //describe table
        sql = "DESCRIBE " + tableName;
        System.out.println("Running " + sql);
        resultSet = state.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2));
        }

        sql = "SELECT * FROM " + tableName;
        System.out.println("Running " + sql);
        resultSet = state.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getInt(1) + "\t" + resultSet.getString(2));
        }

        sql = "SELECT count(*) FROM " + tableName;
        System.out.println("Running " + sql);
        resultSet = state.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }*/

        String sql = "INSERT INTO " + tableName + " VALUES(1,'tt')";
        System.out.println("Running " + sql);
        int cout = state.executeUpdate(sql);
        System.out.println(cout);

    }
}
